package com.orangelife.mobile.app.application;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;


import android.app.Activity;
import android.app.Application;
import android.content.Intent;

public class ExitApplication extends Application {
    private List<Activity> activityList = new LinkedList<>();
    private List<Activity> failureList = new LinkedList<>();
    private HashMap<String, Activity> map = new HashMap<>();
    private static ExitApplication instance;

    /**
     * 所有订单支付的类都要调用ExitApplication.addPaidActivity()，以保证在支付界面可以回退，在支付结果界面可以被统一关闭
     *
     * @param activity 需要在支付结果页被关闭的订单页Activity
     */
    public void addPaidActivity(Activity activity) {
        activityList.add(activity);
    }

    /** 支付结束之后，退出支付结果页之后，关闭之前暂时存留的订单页 */
    public void finishPaidActivity() {
        for (Activity activity : activityList) {
            activity.finish();
        }
    }

    public void clearPaidActivityList() {
        if (activityList != null) {
            activityList.clear();
        }
    }

    public void addFailActivity(Activity activity) {
        failureList.add(activity);
    }

    public void finishFailActivity() {
        for (Activity activity : failureList) {
            activity.finish();
        }
    }

    public static ExitApplication getInstance() {
        if (null == instance) {
            instance = new ExitApplication();
        }
        return instance;
    }

    public Void addActivity(Activity activity) {
        activityList.add(activity);
        return null;
    }

    /**
     * 关闭指定的Activity
     *
     * @param cls
     */
    public void finishAppointActivity(Class<?> cls) {
        for (Activity activitys : activityList) {
            if (activitys.getClass().equals(cls)) {
                finishActivity(activitys);
            }
        }
    }

    /**
     * 结束指定的Activity
     *
     * @param activity
     */
    public void finishActivity(Activity activity) {
        if (activity != null) {
            activity.finish();
            activity = null;
        }
    }

    public void addMainActivity(Activity activity) {
        activityList.add(activity);
    }

    public void addService(String string, Activity activity) {
//		map.put(string, activity);
    }

    public void exit() {
//		CurryApplication.getInstance().setUpdate(false);
        int count = 0;
        for (Activity activity : activityList) {
            count++;
            activity.finish();
        }
        // 遍历键，通过键取值
        Set set = map.keySet();
        for (Object key : set) {
            try {
                map.get(key).stopService(
                        new Intent(map.get(key), Class.forName((String) key)));
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
        }
        deletelastActivity();
    }


    public void deletelastActivity() {
        if (activityList.size() > 0) {
            activityList.remove(activityList.size() - 1);
        }
    }
}
